package com.zm.zmgz.service;

import com.zm.zmgz.dao.EmployeeStatisticsBeanMapper;
import com.zm.zmgz.pojo.EmployeeStatisticsBean;
import com.zm.zmgz.pojo.EmployeeStatisticsBeanExample;
import com.zm.zmgz.pojo.ProjectProfitBean;
import com.zm.zmgz.utils.BigDecimalUtils;
import com.zm.zmgz.utils.DateUtils;
import com.zm.zmgz.utils.StringCjwUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: cjw
 * @Date: 2020/06/01/20:48
 * @Description: 员工统计表业务层
 */
@Service
public class EmployeeStatisticsService {

    @Autowired
    EmployeeService employeeService; // 员工表业务层

    @Autowired
    EmployeeStatisticsBeanMapper employeeStatisticsBeanMapper; // 员工统计表

    @Autowired
    BusinessAnalysisService businessAnalysisService; // 营业分析表业务层

    @Autowired
    ProjectProfitService projectProfitService; // 项目盈利表业务层

    /***
     * @Description: 根据批次id来删除员工统计记录
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-06-01 21:01:54
     */
    public Integer delteEmployeeStatisticsByBatch(Integer batchId) {
        EmployeeStatisticsBeanExample employeeStatisticsBeanExample = new EmployeeStatisticsBeanExample();
        EmployeeStatisticsBeanExample.Criteria criteria = employeeStatisticsBeanExample.createCriteria();
        criteria.andBatchidEqualTo(batchId);
        return employeeStatisticsBeanMapper.deleteByExample(employeeStatisticsBeanExample);
    }


    /***
     * @Description: 根据批次id来添加员工统计记录
     * 通过批次id查询批次
     * 通过批次获取项目盈利记录(项目盈利表的批次是日期类型)
     * 遍历项目盈利记录来设置员工统计对象属性值
     *  每遍历一条记录,生成一个EmployeeStatistics对象,并存储到数据库中
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-06-01 21:06:58
     */
    @Transactional
    public Integer addEmployeeStatisticsByBatchId(Integer batchId) {
        // 通过批次id查询批次
        String batch = businessAnalysisService.getBatchById(batchId);
        // string->date(项目盈利表的批次是日期类型)
        Date date = DateUtils.getDateByString(batch, "yyyy年MM月");
        // 通过批次获取项目盈利记录(项目盈利表的批次是日期类型)
        List<ProjectProfitBean> list = projectProfitService.getBatchByDate(date, 0, 0);
        if (Objects.isNull(list) || list.size() == 0)
            return null;
        // 遍历项目盈利记录来设置员工统计对象属性值
        int i = 0;
        for (ProjectProfitBean profitBean : list) {
            EmployeeStatisticsBean bean = new EmployeeStatisticsBean();
            String name = profitBean.getName();
            // 获取收入
            String posettlementfee = profitBean.getPosettlementfee();
            BigDecimal decimal = BigDecimalUtils.getBigDecimalByString(posettlementfee); // po结费
            String taxrate = profitBean.getTaxrate();
            double v = StringCjwUtils.getDoubleByPercent(taxrate);
            v = 1 - v;
            BigDecimal decimal1 = BigDecimalUtils.getBigDecimalByDouble(v); // 去税后比例
            BigDecimal income = BigDecimalUtils.multiply(decimal, decimal1); // 收入 = po结费 * 去税后比例
            // 获取支出(税前工资+企业缴纳五险一金)
            BigDecimal decimal2 = profitBean.getEnterprisespayfiveinsurancesandonegold(); // 企业缴纳五险一金
            String pretaxwages = profitBean.getPretaxwages();
            BigDecimal decimal3 = BigDecimalUtils.getBigDecimalByString(pretaxwages); // 税前工资
            BigDecimal expenditure = BigDecimalUtils.add(decimal2, decimal3); // 支出
            // 获取利润
            String s = profitBean.getProfit();
            BigDecimal profit = BigDecimalUtils.getBigDecimalByString(s); // 利润
            // 获取状态
            String idcard = profitBean.getIdcard();
            String status = employeeService.getStatusByIdCard(idcard);
            // 设置值
            bean.setBatchid(batchId);
            bean.setName(name);
            bean.setIncome(income);
            bean.setExpenditure(expenditure);
            bean.setProfit(profit);
            bean.setStatus(status);
            // 保存到数据库中
            i += employeeStatisticsBeanMapper.insert(bean);
        }
        return i;
    }

    /***
     * @Description: 根据批次id获取员工统计记录
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-06-01 22:26:39
     */
    public List<EmployeeStatisticsBean> getEmployeeStatisticsByBatchId(Integer batchId) {
        EmployeeStatisticsBeanExample example = new EmployeeStatisticsBeanExample();
        EmployeeStatisticsBeanExample.Criteria criteria = example.createCriteria();
        criteria.andBatchidEqualTo(batchId);
        List<EmployeeStatisticsBean> list = employeeStatisticsBeanMapper.selectByExample(example);
        if (Objects.isNull(list) || list.size() == 0)
            return null;
        else
            return list;
    }


    /***
     * @Description: 判断传入集合中是否存在该name
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-06-02 09:29:02
     */
    public EmployeeStatisticsBean getEmpStatisticsByNameFromList(String name, List<EmployeeStatisticsBean> list) {
        if (Objects.isNull(list) || Objects.isNull(name))
            return null;
        for (EmployeeStatisticsBean bean : list) {
            String name1 = bean.getName();
            if (name.equals(name1))
                return bean;
        }

        return null;
    }
}
